// pages/home/home.js
import {http} from "../../utils/ajax.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    daka:"",
    name:"",
    startTime:"",
    endTime:"",
    time:"",
    qiandao:"签到",
    _id:""
  },
  
  qiandao(){
    var mobile = wx.getStorageSync('mobile')
    var time=this.data.time;
    var name=this.data.name;
    var that = this
    if(this.data.startTime==""){
      http({
        url: "https://shiwenmz.top:1915/xcx/qiandao",
        method: "POST",
        data: {
          mobile,
          name
        },
        success: res => {
          that.setData({
            daka: "今日打卡",
            qiandao: "签退",
            startTime: time,
            _id:res.data.result[0]._id
          })
        }
      })
    }else{
        http({
          
          url: "https://shiwenmz.top:1915/xcx/qiantui",
          method: "POST",
          data: {
            _id: that.data._id
          },
          success: res => {
            var date1 = res.data.result.endTime
            var hours1 = date1.substring(11, 13) * 1 + 8
            var minutes1 = date1.substring(14, 17)
            var seconds1 = date1.substring(17, 19)
            this.setData({
              endTime: hours1 + ":" + minutes1 + seconds1
            })
          }
        })
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var mobile = wx.getStorageSync('mobile')
    if(!mobile){
      wx.navigateTo({
        url: '../login/login',
      })
    }
    var _id = wx.getStorageSync('_id')
    http({
      url:"https://shiwenmz.top:1915/xcx/finduser",
      method:"POST",
      data:{
        _id
      },
      success:res=>{
        this.setData({
          name: res.data.result[0].username
        })
      }
    })
    setInterval(()=> {
        var date = new Date();
      var hour = date.getHours() < 10 ? 0 + "" + date.getHours() : date.getHours();
      var minute = date.getMinutes() < 10 ? 0 + "" +date.getMinutes():date.getMinutes();
      var second = date.getSeconds() < 10 ? 0 + "" + date.getSeconds() : date.getSeconds();
        this.setData({
          time: hour + ":" + minute + ":" + second
        })
    }, 1000) //循环时间 这里是1秒  
    var date=new Date();
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0)
    http({
      url: "https://shiwenmz.top:1915/xcx/finddaka",
      method:"POST",
      data:{
        mobile: wx.getStorageSync('mobile'),
        daoTime:date,
        tuiTime:new Date()
      },
      success:res=>{
        if(res.data.result){
          if (res.data.result.startTime) {
            var date = res.data.result.startTime
            var hours = date.substring(11, 13) * 1 + 8
            var minutes = date.substring(14, 17)
            var seconds = date.substring(17, 19)
            if (res.data.result.endTime) {
              var date1 = res.data.result.endTime
              var hours1 = date1.substring(11, 13) * 1 + 8
              var minutes1 = date1.substring(14, 17)
              var seconds1 = date1.substring(17, 19)
            }
            this.setData({
              startTime: hours + ":" + minutes + seconds,
              endTime: res.data.result.endTime ? hours1 + ":" + minutes1 + seconds1 : "",
              qiandao: "签退",
              _id: res.data.result._id,
              daka: "今日打卡"
            })
          }
        }
        
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})